export const calcDate = (date1, date2) => {
  let date3 = date2 - date1;

  let days = Math.floor(date3 / (24 * 3600 * 1000))

  let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
  let hours = Math.floor(leave1 / (3600 * 1000))

  let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
  let minutes = Math.floor(leave2 / (60 * 1000))

  let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
  let seconds = Math.round(date3 / 1000)
  return {
    leave1,
    leave2,
    leave3,
    days: days,
    hours: hours,
    minutes: minutes,
    seconds: seconds,
  }
}

/**
 * 日期格式化
 */
export function dateFormat(date = new Date(), format) {
  date = new Date(date)
  format = format || 'yyyy-MM-dd hh:mm:ss';
  if (date !== 'Invalid Date') {
    let o = {
      "M+": date.getMonth() + 1, //month
      "d+": date.getDate(), //day
      "h+": date.getHours(), //hour
      "m+": date.getMinutes(), //minute
      "s+": date.getSeconds(), //second
      "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
      "S": date.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
      if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
          RegExp.$1.length === 1 ? o[k] :
            ("00" + o[k]).substr(("" + o[k]).length));
    return format;
  }
  return '';
}

export const dateFormatter = (date = new Date()) => {
  let format = "yyyy-MM-ddThh:mm:ss.000Z";
  date = new Date(date)
  if (date !== 'Invalid Date') {
    let o = {
      "M+": date.getMonth() + 1, //month
      "d+": date.getDate(), //day
      "h+": date.getHours(), //hour
      "m+": date.getMinutes(), //minute
      "s+": date.getSeconds(), //second
      "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
      "S": date.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
      if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
          RegExp.$1.length === 1 ? o[k] :
            ("00" + o[k]).substr(("" + o[k]).length));
    return format;
  }
  return '';
}

// 星期几
export const dateWeek = (date, format = "星期") => {
  var mydate = new Date(date);
  var myddy = mydate.getDay(); //获取存储当前日期
  var weekday = ["日", "一", "二", "三", "四", "五", "六"];
  return format + weekday[myddy]
}

// 计算时间长度
export function formatTime(seconds, util = ["h", "m", "s"]) {
  // 计算小时数
  const hours = Math.floor(seconds / 3600);
  // 计算剩余的秒数
  const remainingSeconds = seconds % 3600;
  // 计算分钟数
  const minutes = Math.floor(remainingSeconds / 60);
  // 计算剩余的秒数
  const secs = (remainingSeconds % 60).toFixed(0);

  let time = "";
  if (hours > 0) {
    time += `${hours}${util[0]} ${minutes}${util[1]} ${secs}${util[2]}`
  } else {
    if (minutes > 0) {
      time += `${minutes}${util[1]} ${secs}${util[2]}`
    } else {
      time += `${secs}${util[2]}`
    }
  }
  return time
}